package com.dqyt.khjc.service;

import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.Maintenance;
import com.dqyt.app.bean.Report;
import com.dqyt.app.bean.Sample;
import com.dqyt.app.bean.WorkBean;
import com.dqyt.app.mongo.MaintenanceDao;
import com.dqyt.app.mongo.ReportDao;
import com.dqyt.app.mongo.SampleDao;
import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.RegularUtil;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.*;
import com.dqyt.khjc.config.Config;
import com.dqyt.khjc.controller.SmsController;
import com.dqyt.khjc.mapper.*;
import com.dqyt.khjc.mongo.WorkDao;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.NumberUtil;
import com.dqyt.khjc.utils.TimeUtil;
import com.dqyt.quart.AddSimpleJobReq;
import com.dqyt.quart.DeleteJobReq;
import com.dqyt.quart.QuartzManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.sql.Time;
import java.util.*;

/**
 * 作业信息管理
 */
@Service
public class WorkService {


    @Autowired
    private WorkMapper workMapper;

    @Autowired
    private HistoryMapper historyMapper;

    @Autowired
    private ReportDao reportDao;

    @Autowired
    private SampleDao sampleDao;

    @Autowired
    private MaintenanceDao maintenanceDao;


    @Autowired
    private QuartzManager quartzManager;

    /**
     * 快速分配任务
     *
     * @param request
     * @return
     */
    public ResultMsg setAssignTasks(HttpServletRequest request,int type, long userId,String startNumberStr,String endNumberStr) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否可以管理待审核申请
//            if (!CheckTool.checkAuthority(user, AuthorityID.ZYGL_KSFPRW)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }

            //如果只有开始号
            if("".equals(endNumberStr)){
                int mybsbh =Integer.parseInt(startNumberStr);
                String mybsbhStr =String.valueOf(mybsbh);
                int mlength = startNumberStr.length() -mybsbhStr.length();
                String zhBsbh = NumberUtil.calculationBsbh(mybsbh,mlength,type);
                //增加
                WorkBean workBean =  new WorkBean();
                workBean.setBsbh(zhBsbh);
                workBean.setMtype(type);
                workBean.setAddTime(TimeUtil.currentTimeMillis());
                workBean.setAddUser(user.getUserId()+"");
                workMapper.addWork(workBean);
            }else{
                int startNumber = Integer.parseInt(startNumberStr);
                int endNumber = Integer.parseInt(endNumberStr);
                if(endNumber<=startNumber){
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("结束号必须大于开始号");
                    return ret;
                }
                int count = (endNumber - startNumber);
                for(int a =0;a<=count;a++){
                    int mybsbh =startNumber+a;
                    String mybsbhStr =String.valueOf(mybsbh);
                    int mlength = startNumberStr.length() -mybsbhStr.length();
                    String zhBsbh = NumberUtil.calculationBsbh(mybsbh,mlength,type);
                    //增加
                    WorkBean workBean =  new WorkBean();
                    workBean.setBsbh(zhBsbh);
                    workBean.setMtype(type);
                    workBean.setAddTime(TimeUtil.currentTimeMillis());
                    workBean.setAddUser(user.getUserId()+"");
                    workMapper.addWork(workBean);
                }
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("任务已生成");



            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "]快速分配了任务：开始号为： [" + startNumberStr + "]结束号为：[" + endNumberStr + "]");
                historyBean.setType(Opt.RWYL_KSFPRW_RWFP);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId("");
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }


        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 分页查询 任务
     * @param request
     * @param page
     * @param size
     * @param rwdbh
     * @param wtfId
     * @param mtype
     * @return
     */
    public ResultMsg getWorkListPage(HttpServletRequest request,Integer page,Integer size,String rwdbh,
                                     long wtfId,int mtype,int workTYpe,int workTYpeCh,int workTYpeCh2,long jyyId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            if (page != null && size != null) {
                page = (page - 1) * size;
            }

            List<WorkBean> workBeans =  workMapper.getWorkListPage(page,size,rwdbh,wtfId,mtype,workTYpe,workTYpeCh,workTYpeCh2,jyyId);
            long total =  workMapper.getWorkListPageTotle(rwdbh,wtfId,mtype,workTYpe,workTYpeCh,workTYpeCh2,jyyId);

            RespPageBean bean = new RespPageBean();
            bean.setData(workBeans);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 分页查询 任务
     * @param request
     * @param page
     * @param size
     * @param rwdbh
     * @param wtfId
     * @param mtype
     * @return
     */
    public ResultMsg getWorkListPagex(HttpServletRequest request,Integer page,Integer size,String rwdbh,
                                     long wtfId,int mtype,int workTYpe,int workTYpeCh,int workTYpeCh2,long jyyId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

            if (page != null && size != null) {
                page = (page - 1) * size;
            }

            List<WorkBean> workBeans =  workMapper.getWorkListPagex(page,size,rwdbh,wtfId,mtype,workTYpe,workTYpeCh,workTYpeCh2,jyyId);
            long total =  workMapper.getWorkListPageTotlex(rwdbh,wtfId,mtype,workTYpe,workTYpeCh,workTYpeCh2,jyyId);

            RespPageBean bean = new RespPageBean();
            bean.setData(workBeans);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 修改任务
     * @param request
     * @param workId
     * @param userId
     * @param bsbh
     * @return
     */
    public ResultMsg updateWorkById(HttpServletRequest request,long workId,long userId,String bsbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            long updateTiem =TimeUtil.currentTimeMillis();
            int res =   workMapper.updateWork(userId, bsbh,workId,updateTiem,user.getUserId()+"");
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("修改成功");
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常！");
            }

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "]修改了标识编号为： [" + bsbh + "]的任务");
                historyBean.setType(Opt.RWYL_KSFPRW_XGRW);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(workId+"");
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 删除任务
     * @param request
     * @param workId
     * @return
     */
    public ResultMsg   deleteWorkById(HttpServletRequest request,long workId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
           int res = workMapper.deleteWork(workId);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("删除成功");
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常！");
            }
            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //添维修记录
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                historyBean.setMessage("[" + user.getUserName() + "]删除了id为： [" + workId + "]的任务");
                historyBean.setType(Opt.RWYL_KSFPRW_SC);
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(workId+"");
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;

    }

    /**
     * 分配任务
     * @param request
     * @param bsbhArrar
     * @param jcrUserId
     * @param jcrStr
     * @return
     */
    public ResultMsg assignJobs(HttpServletRequest request,String [] bsbhArrar,String jcrUserId,String jcrStr){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            if("".equals(jcrUserId) || bsbhArrar.length==0){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.ZYGL_DFP)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            int res = workMapper.updateStateByIDs(bsbhArrar,Long.parseLong(jcrUserId),jcrStr,2);
            if(res>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("未知的错误，请稍候再试！");
                return ret;
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 设置任务状态
     * @param request
     * @param bsbhArrar
     * @param workTYpe
     * @return
     */
    public ResultMsg setworkTYpeYdy(HttpServletRequest request,String [] bsbhArrar,int  workTYpe){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.ZYGL_DDY)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            //将数组转为list
            List<String> listB= new ArrayList<>();
            for(int c=0;c<bsbhArrar.length;c++){
                listB.add(bsbhArrar[c]);
            }


            //创建新列表 用来装充气的但是没有报告的气瓶
            List<String> qpzhuan4 = new ArrayList<>();
            //批量查询是充气的气瓶
            List<Sample> qclist =  sampleDao.getReportByBatchCq(listB);
            for(Sample sample:qclist){
                //查询报告
                Report report =   reportDao.getReportBybsbh(sample.bsbh,2);
                if(null == report){
                    qpzhuan4.add(sample.bsbh);
                }
            }

             //将是充气的 但是没有报告的直接转到 待装箱 但是已完成

           int res = workMapper.setworkTYpeYzxTo4(bsbhArrar);

            for(int i = listB.size() - 1; i >= 0; i--){
                String item = listB.get(i);
                if(qpzhuan4.contains(item)){
                    listB.remove(item);
                }
            }
            if(listB.size()>0){
                String[] mbsbhArr = new String[listB.size()];
                listB.toArray(mbsbhArr);
                int res3 = workMapper.setworkTYpeYdy(mbsbhArr,workTYpe);
                if(res3>0){
                    ret.setCode(ResultMsg.SUCESS);
                    ret.setMsg("操作成功");
                    return ret;
                }else{
                    ret.setCode(ResultMsg.ERROR);
                    ret.setMsg("未知的错误，请稍候再试！");
                    return ret;
                }
            }else{
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                return ret;
            }



        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 设置任务状态 已完成
     * @param request
     * @param bsbhArrar
     * @return
     */
    public ResultMsg setworkTYpeYwc(HttpServletRequest request,String [] bsbhArrar){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.ZYGL_DWC)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            //更新已完成并且是已装箱的
            long res =  workMapper.updateWcOrYzxList(bsbhArrar);
            //更新已完成单是未装箱的
            long res2 = workMapper.updateYwcOrWzxList(bsbhArrar);
            if(res>0 || res2>0 ){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("未知的错误，请稍候再试！");
                return ret;
            }
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 设置任务状态 已装箱
     * @param request
     * @param bsbhArrar
     * @return
     */
    public ResultMsg setworkTYpeYzx(HttpServletRequest request,String [] bsbhArrar){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.ZYGL_DZX)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            //更新已装箱 并且是已完成的
            long res =  workMapper.updateYzxOrYwcList(bsbhArrar);
            long res2 = workMapper.updateYzxOrWwcList(bsbhArrar);
            if(res>0 || res2>0 ){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("未知的错误，请稍候再试！");
                return ret;
            }
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 设置任务状态 已发放
     * @param request
     * @param bsbhArrar
     * @return
     */
    public ResultMsg setworkTYpeYff(HttpServletRequest request,String [] bsbhArrar){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            // 用户权限检查 是否有客户信息一览的权限
//            if (!CheckTool.checkAuthority(user, AuthorityID.ZYGL_DFF)) {
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("你没有对应的操作权限");
//                return ret;
//            }
            //更新已装箱 并且是已完成的
            long res =  workMapper.setworkTYpeYff(bsbhArrar);
            if(res>0 ){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("操作成功");
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("未知的错误，请稍候再试！");
                return ret;
            }
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 工作量确认单
     * 根据任务单号  查询统计 已完成的 气瓶面罩 ，已维修的气瓶，面罩，充气 等等
     * @param request
     * @param rwdbh  任务单编号
     * @return
     */
    public ResultMsg  getGzlqrdByRwdbh(HttpServletRequest request,String rwdbh){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            //根据条件号查询 work表
            List<WorkBean> mw =  workMapper.getWorkStatistics(rwdbh,-1,-1,0,0,0);
            //把所有的标识编号都取出来
            List<String> bsbhList =  new ArrayList<>();
            for(WorkBean workBean :mw){
                bsbhList.add(workBean.getBsbh());
            }


//            //根据 任务单编号和类型查询 已完成的报告 面罩的个数统计
//            List<Report> mzNumbers = reportDao.getReportByrwdbh(rwdbh,1);
//            int numberMzJm=0;
//            for(Report report:mzNumbers){
//               //除去维修替换了 背板/减压器
//               boolean isbh =  report.bsbh.contains("_old");
//               if(!isbh){
//                   numberMzJm++;
//               }
//            }

//            //根据 任务单编号和类型查询 已完成的报告 气瓶的个数统计
//            List<Report> qpNumbers = reportDao.getReportByrwdbh(rwdbh,2);
//            int numberQpJm=0;
//            for(Report report2:qpNumbers){
//                //除去维修替换了 背板/减压器
//                boolean isbh =  report2.bsbh.contains("_old");
//                if(!isbh){
//                    numberQpJm++;
//                }
//            }

//            //统计充气的气瓶有多少个
//            List<Report> msamples=  reportDao.getReportByrwdbhOrCq(rwdbh);
//            int numberQpCqJm =0;
//            //维修
//            for(Report mrport:msamples){
//                //除去维修替换了 背板/减压器
//                boolean isbh =  mrport.bsbh.contains("_old");
//                if(!isbh){
//                    numberQpCqJm++;
//                }
//            }

            //然后查询已完成的报告 面罩的个数统计
            List<Report> mzNumbers = reportDao.getReportByBatch(bsbhList,1);
            int numberMzJm=0;
            for(Report report:mzNumbers){
                //除去维修替换了 背板/减压器
                boolean isbh =  report.bsbh.contains("_old");
                if(!isbh){
                    numberMzJm++;
                }
            }

            //根据 任务单编号和类型查询 已完成的报告 气瓶的个数统计
            List<Report> qpNumbers = reportDao.getReportByBatch(bsbhList,2);
            int numberQpJm=0;
            for(Report report2:qpNumbers){
                //除去维修替换了 背板/减压器
                boolean isbh =  report2.bsbh.contains("_old");
                if(!isbh){
                    numberQpJm++;
                }
            }

            //统计充气的气瓶有多少个
            //List<Report> msamples=  reportDao.getReportByrwdbhOrCqBath(bsbhList);
            List<Sample> msamples=  sampleDao.getReportByrwdbhOrCqBath(bsbhList);
            int numberQpCqJm =0;
            //维修
            for(Sample mSample:msamples){
                //除去维修替换了 背板/减压器
                boolean isbh =  mSample.bsbh.contains("_old");
                if(!isbh){
                    numberQpCqJm++;
                }
            }



            //查询维修个数
            List<Maintenance>  maintenances=  maintenanceDao.getMaintenanceByRwdbh(rwdbh);

            HashSet<String> bjmcSet = new  HashSet<String>();
            for(Maintenance maiobj:maintenances){
                if(!"".equals(maiobj.mzGhbjpp)){
                    bjmcSet.add(maiobj.mzGhbjpp);
                }

                if(!"".equals(maiobj.gqfGhbjpp)){
                    bjmcSet.add(maiobj.gqfGhbjpp);
                }

                if(!"".equals(maiobj.bjqGhbjpp)){
                    bjmcSet.add(maiobj.bjqGhbjpp);
                }

                if(!"".equals(maiobj.ylbGhbjpp)){
                    bjmcSet.add(maiobj.ylbGhbjpp);
                }

                if(!"".equals(maiobj.jyqGhbjpp)){
                    bjmcSet.add(maiobj.jyqGhbjpp);
                }
                if(!"".equals(maiobj.bbGhbjpp)){
                    bjmcSet.add(maiobj.bbGhbjpp);
                }

                if(!"".equals(maiobj.pfGhbjpp)){
                    bjmcSet.add(maiobj.pfGhbjpp);
                }

                if(!"".equals(maiobj.qpGhbjpp)){
                    bjmcSet.add(maiobj.qpGhbjpp);
                }

                if(!"".equals(maiobj.xzGhbjpp)){
                    bjmcSet.add(maiobj.xzGhbjpp);
                }

            }

            GzltjdDetailBean gzltjdDetailBean = new GzltjdDetailBean();
            gzltjdDetailBean.setMzNumber(numberMzJm);
            gzltjdDetailBean.setQpNumber(numberQpJm);
            gzltjdDetailBean.setCqNumber(numberQpCqJm);
            gzltjdDetailBean.setWxNumber(maintenances.size());
            gzltjdDetailBean.setMaintenances(maintenances);
            gzltjdDetailBean.setBjmcSet(bjmcSet);






            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(gzltjdDetailBean);

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }











    /**
     * 工作量统计单
     * 根据任务单号  查询统计 已完成的 气瓶面罩 ，已维修的气瓶，面罩，充气 等等
     * @param request
     * @param rwdbh  任务单编号
     * @return
     */
    public ResultMsg  getGzltjdByRwdbh(HttpServletRequest request,String rwdbh,String sTime,String eTime,
                                       String wtfIdStr,long wtfchId,String jyyIdStr){
        List<Sample> sampleList = new ArrayList<Sample>();
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {



            long startTime=0;
            long endTime =0;
            if(!"".equals(sTime)){
                startTime = TimeUtil.formatDate_5(sTime);
            }
            //
            if(!"".equals(eTime)){
                endTime = TimeUtil.formatDate_5(eTime);
            }


            long wtfId=-1;
            long jyyId=0;
            if(!"".equals(wtfIdStr)){
                wtfId = Long.parseLong(wtfIdStr);
            }

            if(!"".equals(jyyIdStr)){
                jyyId = Long.parseLong(jyyIdStr);
            }

            if("".equals(rwdbh)){
                rwdbh = "-1";
            }

            //根据条件号查询 work表
            List<WorkBean> mw =  workMapper.getWorkStatistics(rwdbh,wtfId,wtfchId,startTime,endTime,jyyId);

            //把所有的标识编号都取出来
            List<String> bsbhList =  new ArrayList<>();
            for(WorkBean workBean :mw){
                bsbhList.add(workBean.getBsbh());
            }


            //然后查询已完成的报告 面罩的个数统计
            List<Report> mzNumbers = reportDao.getReportByBatch(bsbhList,1);
            int numberMzJm=0;
            for(Report report:mzNumbers){
                //除去维修替换了 背板/减压器
                boolean isbh =  report.bsbh.contains("_old");
                if(!isbh){
                    numberMzJm++;
                }
            }

            //根据 任务单编号和类型查询 已完成的报告 气瓶的个数统计
            List<Report> qpNumbers = reportDao.getReportByBatch(bsbhList,2);
            int numberQpJm=0;
            for(Report report2:qpNumbers){
                //除去维修替换了 背板/减压器
                boolean isbh =  report2.bsbh.contains("_old");
                if(!isbh){
                    numberQpJm++;
                }
            }

            //统计充气的气瓶有多少个
            //List<Report> msamples=  reportDao.getReportByrwdbhOrCqBath(bsbhList);
            List<Sample> msamples = sampleDao.getReportByrwdbhOrCqBath(bsbhList);

            int numberQpCqJm =0;
            //维修
            for(Sample mSample:msamples){
                //除去维修替换了 背板/减压器
                boolean isbh =  mSample.bsbh.contains("_old");
                if(!isbh){
                    numberQpCqJm++;
                }
            }
            //查询维修个数
            List<Maintenance>  maintenances=  maintenanceDao.getMaintenanceByRwdbhBath(bsbhList);

            HashSet<String> bjmcSet = new  HashSet<String>();
            for(Maintenance maiobj:maintenances){
                if(!"".equals(maiobj.mzGhbjpp)){
                    bjmcSet.add(maiobj.mzGhbjpp);
                }

                if(!"".equals(maiobj.gqfGhbjpp)){
                    bjmcSet.add(maiobj.gqfGhbjpp);
                }

                if(!"".equals(maiobj.bjqGhbjpp)){
                    bjmcSet.add(maiobj.bjqGhbjpp);
                }

                if(!"".equals(maiobj.ylbGhbjpp)){
                    bjmcSet.add(maiobj.ylbGhbjpp);
                }

                if(!"".equals(maiobj.jyqGhbjpp)){
                    bjmcSet.add(maiobj.jyqGhbjpp);
                }
                if(!"".equals(maiobj.bbGhbjpp)){
                    bjmcSet.add(maiobj.bbGhbjpp);
                }

                if(!"".equals(maiobj.pfGhbjpp)){
                    bjmcSet.add(maiobj.pfGhbjpp);
                }

                if(!"".equals(maiobj.qpGhbjpp)){
                    bjmcSet.add(maiobj.qpGhbjpp);
                }

                if(!"".equals(maiobj.xzGhbjpp)){
                    bjmcSet.add(maiobj.xzGhbjpp);
                }

            }

            GzltjdDetailBean gzltjdDetailBean = new GzltjdDetailBean();
            gzltjdDetailBean.setMzNumber(numberMzJm);
            gzltjdDetailBean.setQpNumber(numberQpJm);
            gzltjdDetailBean.setCqNumber(numberQpCqJm);
            gzltjdDetailBean.setWxNumber(maintenances.size());
            gzltjdDetailBean.setMaintenances(maintenances);
            gzltjdDetailBean.setBjmcSet(bjmcSet);

            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("查询成功");
            ret.setData(gzltjdDetailBean);

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     * 工作量 统计的分页查询任务
     * @param request
     * @param page
     * @param size
     * @param rwdbh
     * @param wtfId
     * @return
     */
    public ResultMsg getWorkStatisticsPage(HttpServletRequest request,Integer page,Integer size,String rwdbh,String startTimeStr,String endTimeStr,
                                     long wtfId,long wtfchId,long jyyId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            if (page != null && size != null) {
                page = (page - 1) * size;
            }

//            if(!"-1".equals(wtfId) && !"".equals(wtfmh)){
//                ret.setCode(ResultMsg.ERROR);
//                ret.setMsg("委托方和和委托方模糊查询不能同时存在");
//                return ret;
//            }


            long startTime=0;
            long endTime =0;
            if(!"".equals(startTimeStr)){
                startTime = TimeUtil.formatDate_5(startTimeStr);
            }
            //
            if(!"".equals(endTimeStr)){
                endTime = TimeUtil.formatDate_5(endTimeStr);
            }
            //查询的是work表
            List<WorkBean> workBeans =  workMapper.getWorkStatisticsPage(page,size,rwdbh,wtfId,wtfchId,startTime,endTime,jyyId);
            long total =  workMapper.getWorkStatisticsTotle(rwdbh,wtfId,wtfchId,startTime,endTime,jyyId);


//            //分页查询数据
//            List<Report> samples = reportDao.getReportListPage4(page, size, rwdbh,wtfId,startTime,endTime,jyyId);
//            //查询总条数
//            long total = reportDao.getReportListTotle4(page, size, rwdbh,wtfId,startTime,endTime,jyyId);

            RespPageBean bean = new RespPageBean();
            bean.setData(workBeans);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }





    /**
     * 工作量 统计的分页查询任务
     * @param request
     * @param page
     * @param size
     * @param rwdbh
     * @param wtfId
     * @return
     */
    public ResultMsg getWorkStatistics2(HttpServletRequest request,Integer page,Integer size,String rwdbh,String startTimeStr,String endTimeStr,
                                           long wtfId,long wtfchId,long jyyId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            if (page != null && size != null) {
                page = (page - 1) * size;
            }

            long startTime=0;
            long endTime =0;
            if(!"".equals(startTimeStr)){
                startTime = TimeUtil.formatDate_5(startTimeStr);
            }
            //
            if(!"".equals(endTimeStr)){
                endTime = TimeUtil.formatDate_5(endTimeStr);
            }
            //查询的是work表
            List<WorkBean> workBeans =  workMapper.getWorkStatistics2(page,size,rwdbh,wtfId,wtfchId,startTime,endTime,jyyId);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(workBeans);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }





    /**
     * 台账分页查询任务
     * @param request
     * @param page
     * @param size
     * @param wtfId
     * @return
     */
    public ResultMsg getTzStatisticsPage(HttpServletRequest request,Integer page,Integer size,String rwdbh,String startTimeStr,String endTimeStr,
                                           long wtfId,long wtfchId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 权限检查
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);
            if (page != null && size != null) {
                page = (page - 1) * size;
            }
            long startTime=0;
            long endTime =0;
            if(!"".equals(startTimeStr)){
                startTime = TimeUtil.formatDate_5(startTimeStr);
            }
            //
            if(!"".equals(endTimeStr)){
                endTime = TimeUtil.formatDate_5(endTimeStr);
            }
            //查询的是work表
            List<WorkBean> workBeans =  workMapper.getTzStatisticsPage(page,size,rwdbh,wtfId,wtfchId,startTime,endTime);
            long total =  workMapper.getTzStatisticsTotle(rwdbh,wtfId,wtfchId,startTime,endTime);




            RespPageBean bean = new RespPageBean();
            bean.setData(workBeans);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     * 分页查询 任务分配
     * @param request
     * @param page
     * @param size
     * @param rwdbh
     * @param mtype
     * @return
     */
    public ResultMsg getWorkListPageAll(HttpServletRequest request,Integer page,Integer size,String rwdbh,long wtfId,int wtfchId,int mtype,int workTYpe,long jyyId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            if (page != null && size != null) {
                page = (page - 1) * size;
            }

            int  workTYpeCh=0;
            int workTYpeCh2=0;

            if(workTYpe==88){
                workTYpe = 4;
                workTYpeCh = 1;
                workTYpeCh2=0;
            }else if(workTYpe==99){
                workTYpe = 4;
                workTYpeCh2=1;
                workTYpeCh=0;
            }
            List<WorkBean> workBeans =  workMapper.getAdWorkListPageAll( page, size, rwdbh, wtfId,wtfchId, mtype, workTYpe, workTYpeCh, workTYpeCh2,jyyId);
            long total = workMapper.getAdWorkListPageAllTotle(rwdbh, wtfId,wtfchId, mtype, workTYpe, workTYpeCh, workTYpeCh2,jyyId);

            RespPageBean bean = new RespPageBean();
            bean.setData(workBeans);
            bean.setTotal(total);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(bean);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 检验任务单
     * @param request
     * @param rwdbh
     * @return
     */
    public ResultMsg getJyrwdData(HttpServletRequest request,String rwdbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            List<WorkBean> workBeans =  workMapper.getWorkListByRwdbh(rwdbh);

            JyrwdBean jyrwdBean = new JyrwdBean();

            HashSet<String> hashSet = new HashSet<>();
            //首选找出所有的检验员
            for(WorkBean workBean:workBeans){
                if(null != workBean.getJyy() && !"".equals(workBean.getJyy())){
                    hashSet.add(workBean.getJyy());
                }
            }

            List<String> list2 = new ArrayList<String> ();
            list2.addAll(hashSet);
            List<YuanGongBean> yuanGongBeans = new ArrayList<>();

            for(String str: list2){
                YuanGongBean yuanGongBean = new YuanGongBean();
                yuanGongBean.ygName = str;
                //面罩数量
                int mnunMZ =0;
                //气瓶数量
                int mnunQP =0;
                String numberStr="";
                HashSet<String> binaho = new HashSet<String>();

                for(int i =0;i<workBeans.size();i++){
                    WorkBean mben =workBeans.get(i);
                    if(str.equals(mben.getJyy())){
                        String m_bsbh=mben.getBsbh();
                        numberStr = numberStr+m_bsbh+",";

                        String mtype=m_bsbh.substring(4,8);
                        if("YTQP".equals(mtype)){ //气瓶
                            mnunQP++;
                        }else{  //面罩
                            mnunMZ++;
                        }
                        binaho.add(m_bsbh.substring(8,m_bsbh.length()));
                    }
                }


                List<String> biaohaoArr = new ArrayList<String>();
                for (String  mint : binaho) {
                    biaohaoArr.add(mint);
                }
                //升序排序
                Collections.sort(biaohaoArr);
                yuanGongBean.bhArray = biaohaoArr;
                yuanGongBean.mzSl = mnunMZ;
                yuanGongBean.qpSl = mnunQP;
                yuanGongBeans.add(yuanGongBean);
            }
            jyrwdBean.yuanGongBeans = yuanGongBeans;
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(jyrwdBean);
            ret.setMsg("成功");
            return ret;
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    public ResultMsg   updateJyyById(HttpServletRequest request,long workId,String jyy,long jyyId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_Web(request);
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            // 首先取得用户信息
            User user = UserInfoUtil.getUser_Web(request);

           int res =  workMapper.updateJyyById(workId,jyy,jyyId);
           if(res>0){
               ret.setCode(ResultMsg.SUCESS);
               ret.setMsg("操作成功");
           }else{
               ret.setCode(ResultMsg.ERROR);
               ret.setMsg("数据异常，请稍候再试");
           }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


}
